package com.jwthhealth_pub.old.sign.presenter;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.sign.model.FindPwModle;
import com.jwthhealth_pub.old.sign.model.SetNewPwModle;
import com.jwthhealth_pub.old.sign.view.SignPwActivity;
import com.jwthhealth_pub.old.signutil.model.SendSms;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by liuzhen000 on 2016/11/25.
 */

public class ISignPwPresenterCompl implements ISignPwPresenter {

    private Context mContext;
    private SignPwActivity mActivity;

    public ISignPwPresenterCompl(Context mContext) {
        this.mContext = mContext;
        this.mActivity = (SignPwActivity) mContext;
    }

    @Override
    public void checkUser(String userName) {
    }

    @Override
    public void findPw(String phone, String code) {

        Call<FindPwModle> call = ApiHelper.findPw(phone, code);
        call.enqueue(new Callback<FindPwModle>() {
            @Override
            public void onResponse(Call<FindPwModle> call, Response<FindPwModle> response) {
                FindPwModle findPwModle = response.body();
                if (findPwModle.getCode().equals("0")){
                    Log.d("ISignInPresenterCompl", "sign up signInSuccess");
                    Message message = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(mActivity.PWMOUDLE, findPwModle);
                    message.setData(bundle);
                    message.what = mActivity.HIDELAYOUTOPT;
                    mActivity.isRequesting = false;
                    mActivity.progress();
                    mActivity.mHandler.sendMessage(message);
                } else {
                    Log.d("ISignInPresenterCompl", "sign up fail");
                    mActivity.isRequesting = false;
                    mActivity.progress();
                }
            }

            @Override
            public void onFailure(Call<FindPwModle> call, Throwable t) {
                mActivity.isRequesting = false;
                mActivity.progress();
                Log.d("ISignInPresenterCompl", "get fail");
            }
        });


    }

    @Override
    public void setNewPw(String phone, String token, String pw) {

        mActivity.progress();
        Call<SetNewPwModle> call = ApiHelper.setNewPw(phone, token, pw);
        call.enqueue(new Callback<SetNewPwModle>() {
            @Override
            public void onResponse(Call<SetNewPwModle> call, Response<SetNewPwModle> response) {
                Log.d("ISignPwPresenterCompl", call.request().url().toString());
                SetNewPwModle setNewPwModle = response.body();
                if (setNewPwModle.getCode().equals("0")){
                    mActivity.isRequesting = false;
                    mActivity.progress();
                    mActivity.mHandler.sendEmptyMessage(mActivity.CHANGEDSUCCESS);
                } else {
                    String msg = setNewPwModle.getMsg();
                    mActivity.isRequesting = false;
                    mActivity.progress();
                    Log.d("ISignInPresenterCompl", "sign up fail");
                }
            }

            @Override
            public void onFailure(Call<SetNewPwModle> call, Throwable t) {
                mActivity.isRequesting = false;
                mActivity.progress();
                Log.d("ISignInPresenterCompl", "get fail");
            }
        });
    }

    @Override
    public void smsCode(String phone, String type) {

        String sms = ApiHelper.getSms(phone, type);

        Call<SendSms> sendSmsCall = ApiHelper.sendSms(phone, type);
        sendSmsCall.enqueue(new Callback<SendSms>() {
            @Override
            public void onResponse(Call<SendSms> call, Response<SendSms> response) {

                String msg = null;

                if (response.body().getCode().equals("0")) {
                    msg = response.body().getMsg();
                    Message message = new Message();
                    message.what = mActivity.SMSCODE;
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.SIGN_DATA, msg);
                    Log.d("ISignPwPresenterCompl","2 " +msg);
//                    mActivity.mHandler.sendMessage(message);
                } else {
                    msg = response.body().getMsg();
                    Message message = new Message();
                    message.what = mActivity.SMSCODE;
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.SIGN_DATA, "网络错误");
                    Log.d("ISignPwPresenterCompl", "1 " + msg);
//                    mActivity.mHandler.sendMessage(message);
                }

                final String msg1 = msg;

                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d("ISignPwPresenterCompl", msg1);
                        if (msg1 != null && msg1.length()>0){
                            Toast.makeText(mContext, msg1, Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }

            @Override
            public void onFailure(Call<SendSms> call, Throwable t) {
                Message message = new Message();
                message.what = mActivity.SMSCODE;
                Bundle bundle = new Bundle();
                bundle.putString(Constant.SIGN_DATA, "net error");
                Log.d("ISignPwPresenterCompl", "网络错误");
//                mActivity.mHandler.sendMessage(message);
            }
        });
    }

}
